<?php

class ApiDoc
{
    public function getMCAction()
    {
        $module_dir = self::getAppDir();
        $modules = scandir($module_dir);
        unset($modules[0], $modules[1]);

        $m_and_c = [];
        foreach ($modules as $module) {
            $controllers = \Doc\DirParse::getFiles($module_dir . $module . DIRECTORY_SEPARATOR . 'controllers');
            foreach ($controllers as $controller) {
                if ('Controller.php' != substr($controller, -14)) {
                    continue;
                }

                $m_and_c[$module][] = substr($controller, 0, -14);
            }
        }

        unset($m_and_c['examples']);
        self::Ret($m_and_c);
    }

    public function getAAction()
    {
        $_REQUEST[''];
        $module = $_REQUEST['module'];
        $controller = $_REQUEST['controller'];
        if (empty($module) || empty($controller)) {
            return self::Ret('params empty!');
        }

        $file_dir = self::getAppDir() . $module . DIRECTORY_SEPARATOR .
            'controllers' . DIRECTORY_SEPARATOR . $controller . 'Controller.php';

        $act = \Doc\FileParse::getPublicFunDoc($file_dir);

        self::Ret($act);
    }

    private static function Ret($str)
    {
        header('Content-Type:application/json; charset=utf-8');
        echo json_encode($str, JSON_UNESCAPED_UNICODE);
    }

    private static function getAppDir()
    {
        return APPS_ROOT . 'apps' . DIRECTORY_SEPARATOR;
    }
}